import { contextBridge, ipcRenderer } from 'electron';
import { electronAPI } from '@electron-toolkit/preload';

const api = {
  setWindowSize: (item: { width?: number; height?: number } = {}) =>
    ipcRenderer.send('set-window-size', item),
  setCaptureWindowSize: (item: any = {}) =>
    ipcRenderer.send('set-capture-window-size', item),
  childrenWindow: (item: any) => ipcRenderer.send('children-window', item),
  onChildrenWindow: (callback: (data: any) => void) =>
    ipcRenderer.on('children-window-call', (_, arg) => callback(arg)),
  routerBack: () => ipcRenderer.send('router-back'),
  catchScreenCapture: (data: any) =>
    ipcRenderer.send('catch-screen-capture', data),
  onCatchScreenCapture: (callback: (data: any) => void) =>
    ipcRenderer.on('catch-screen-capture-call', (_, arg) => callback(arg))
};

if (process.contextIsolated) {
  try {
    contextBridge.exposeInMainWorld('electron', electronAPI);
    contextBridge.exposeInMainWorld('api', api);
  } catch (error) {
    console.error(error);
  }
} else {
  // @ts-ignore (define in dts)
  window.electron = electronAPI;
  // @ts-ignore (define in dts)
  window.api = api;
}
